﻿#include "LoginController.h"
#include "AgreementController.h"
#include "IndexWindow.h"

LoginController::LoginController(){}

LoginController::~LoginController(){}

void LoginController::viewDidLoad(){
	CAView* view=this->getView();
	CAImageView* imageView=CAImageView::createWithLayout(DLayout(DHorizontalLayout_W_C(266, 0.5), DVerticalLayout_H_C(264, 0.2)));
	imageView->setImage(CAImage::create("img/logo.png"));
	view->addSubview(imageView);
	CAFont bigFont;
	bigFont.bold=true;
	bigFont.fontSize=60;
	bigFont.color=themeColor;
	CALabel* label=CALabel::createWithLayout(DLayout(DHorizontalLayoutFill, DVerticalLayout_H_C(100, 0.35)));
	label->setText("1+2数字农业");
	label->setFont(bigFont);
	label->setTextAlignment(CATextAlignment::Center);
	view->addSubview(label);
	CATextField* phone=CATextField::createWithLayout(DLayout(DHorizontalLayout_L_R(100, 100), DVerticalLayout_H_C(100, 0.5)));
	phone->setPlaceHolderText("请输入账号");
	phone->setKeyboardType(CATextField::KeyboardType::PhonePad);
	view->addSubview(phone);
	phone->setText("15966412927");
	CATextField* pass=CATextField::createWithLayout(DLayout(DHorizontalLayout_L_R(100, 100), DVerticalLayout_H_C(100, 0.6)));
	pass->setPlaceHolderText("请输入密码");
	pass->setSecureTextEntry(true);
	pass->setMarginImageRight(DSize(62, 46), "img/see.png");
	view->addSubview(pass);
	pass->setText("123456");
	CAButton* see=(CAButton*)pass->getSubviewByTag(2);
	see->addTarget([pass]{
		if(pass->isSecureTextEntry()){
			pass->setSecureTextEntry(false);
			pass->setMarginImageRight(DSize(64, 52), "img/nosee.png");
		}else{
			pass->setSecureTextEntry(true);
			pass->setMarginImageRight(DSize(62, 46), "img/see.png");
		}
	}, CAButton::Event::TouchDown);
	CAButton* login=CAButton::createWithLayout(DLayout(DHorizontalLayout_L_R(100, 100), DVerticalLayout_H_C(100, 0.75)), CAButton::Type::RoundedRect);
	login->setTitleForState(CAControl::State::Normal, "登  录");
	login->setTitleFontSize(80);
	login->setTitleColorForState(CAControl::State::Normal, CAColor4B::WHITE);
	login->setBackgroundImageForState(CAControl::State::Normal, CAImage::createWithColor4B(themeColor), true);
	login->addTarget([=]{
		if(!box->isOn()){
			getAlert(view, "请同意用户协议");
			return;
		}
		CSJson::Value paras;
		paras["phone"]=phone->getText();
		paras["password"]=pass->getText();
		CSJson::FastWriter writer;
		sendHttp(view, "/authApp/appLogin", writer.write(paras), CALLBACK_BIND_1(LoginController::fillData, this), CAHttpRequest::Type::Post, true);
	}, CAButton::Event::TouchDown);
	view->addSubview(login);
	float left=(view->getBounds().size.width-400)/2;
	box=CACheckbox::createWithLayout(DLayout(DHorizontalLayout_L_W(left, 200), DVerticalLayout_H_C(54, 0.85)), CACheckbox::Type::RoundedRect);
	box->setTitleStateNormal("同意");
	view->addSubview(box);
	CAButton* agreement=CAButton::createWithLayout(DLayout(DHorizontalLayout_L_W(left+140, 240), DVerticalLayout_H_C(54, 0.85)), CAButton::Type::Custom);
	agreement->setTitleForState(CAControl::State::Normal, "《用户协议》");
	agreement->setTitleFontSize(40);
	agreement->setTitleColorForState(CAControl::State::Normal, themeColor);
	agreement->addTarget([&]{
		this->presentModalViewController(AgreementController::create(), true);
	}, CAButton::Event::TouchDown);
	view->addSubview(agreement);
}

void LoginController::fillData(const string& data){
	CSJson::Value value;
	CSJson::Reader reader;
	bool flag=reader.parse(data, value);
	if(!flag){
		getAlert(this->getView(), "数据解析异常, 请稍后重试");

		return;
	}
	authorization=obj2str("Authorization: %s", value["token"].asCString());
	userId=value["user"]["user"]["id"].asUInt();
	nickName=value["user"]["user"]["nickName"].asString();
	
	CAApplication* application=CAApplication::getApplication();
	application->purgeCachedData();
	application->runWindow(RootWindow::getInstance());
	IndexWindow::destroyInstance();
}